package com.java.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.java.entity.Deposit;
import com.java.entity.Money;
import com.java.entity.User;
import com.java.service.impl.DepositServiceImpl;
import com.java.service.impl.UserServiceImpl;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/deposit/")
public class DepositController {
	@Autowired
	private UserServiceImpl us;
	@Autowired
	private DepositServiceImpl ds;
	
	
	@ResponseBody // 用来返回特定的值
	@RequestMapping("user")
	@Transactional
	public ModelAndView userSendDposit(String username, Float deposit) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/deposit/recharge.jsp");// 付款界面

		User u = new User();
		u = us.selectByName(username);

		if (u == null) {
			mav.getModel().put("status", "不存在这个人");
		} else {
			Deposit d = new Deposit();
			d.setAmout(deposit);
			d.setUsername(username);
			ds.insert(d);
			mav.getModel().put("status", "审核中，请耐心等待");
			System.out.println(d);
		}
		return mav;
	}
	
	
	@ResponseBody // 用来返回特定的值
	@RequestMapping("all")
	public String userSendDposit(){
		
		List<Deposit> m = ds.all();
		JSONArray json = JSONArray.fromObject(m);
		return json.toString();
	}
	
	@ResponseBody // 用来返回特定的值
	@RequestMapping("check_deposit")
	@Transactional
	public ModelAndView passMav() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/deposit/checkmoney.jsp");// 付款界面
		List<Deposit> m = ds.all();
		mav.getModel().put("list", m);
		return mav;
	}
	
	
	
	@ResponseBody // 用来返回特定的值
	@RequestMapping("pass")
	@Transactional
	public Integer pass(Integer id) {

		Deposit checkDeposit = new Deposit();
		User u = new User();
		checkDeposit = ds.selectById(id);
		if (checkDeposit.getPass().equals(true)) {
			u = us.selectByName(checkDeposit.getUsername());
			System.out.println(u.getDeposit());
			return -1;
		} else {

			Deposit d = new Deposit();
			d = ds.selectById(id);			
			d.setPass(true);			
			u = us.selectByName(checkDeposit.getUsername());
			float oAmont = u.getMoney();
			u.setMoney(d.getAmout()+oAmont);	 
			us.updateByPrimaryKeySelective(u); 		
			return ds.update(d);
		}	
	}
	
	
	
}
